package cmd

import (
	"context"
	"fmt"
	"github.com/spf13/cobra"
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"time"
)

// Namespace 命令
var namespaceRootCmd = cobra.Command{
	Use:     "namespace",
	Aliases: []string{"ing"},
	Short:   "namespace is used to manage kubernetes namespace",
}

// Namespace Get 命令
var namespaceGetCmd = cobra.Command{
	Use:   "get",
	Short: "get namespace or namespace list",
	Run: func(cmd *cobra.Command, args []string) {
		// 创建 Kubernetes 客户端对象
		k8sClient, err := createK8SClient()
		if err != nil {
			fmt.Println("Err:", err)
			return
		}
		// 调用 List 接口获取 namespace 列表
		nsList, err := k8sClient.CoreV1().Namespaces().List(context.Background(),metav1.ListOptions{})
		if err != nil {
			fmt.Println("Err:", err)
			return
		}

		// 遍历 namespace List，显示 namesapce 信息
		printFmt := "%-30s\t%-10s\t%s\n"
		fmt.Printf(printFmt, "NAME", "STATUS", "AGE")
		for _, ns := range nsList.Items {
			// 打印输出
			fmt.Printf(printFmt, ns.GetName(),
				ns.Status.Phase,
				time.Now().Sub(ns.GetObjectMeta().GetCreationTimestamp().Time).String())

		}
	},
}




